package cn.schoolwow.quickdao.module.database.parent.flow;

import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.module.database.parent.domain.GetStatementSupplier;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class GetAndCacheStatementFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);
        String sqlCacheName = (String) flowContext.checkData("sqlCacheName");

        if(quickDAOConfig.databaseContext.statementCache.contains(sqlCacheName)){
            flowContext.putData("sql", quickDAOConfig.databaseContext.statementCache.get(sqlCacheName));
        }else{
            GetStatementSupplier getStatementSupplier = flowContext.checkInstanceData(GetStatementSupplier.class);
            String sql = getStatementSupplier.getStatement();
            quickDAOConfig.databaseContext.statementCache.put(sqlCacheName, sql);
            flowContext.putData("sql", sql);
        }
    }

    @Override
    public String name() {
        return "获取并缓存SQL语句";
    }
}
